﻿DECLARE @Var_Flag INT
DECLARE @Var_ReservationDate DATE
DECLARE @Var_FirstDeptCode VARCHAR(100)

SET @Var_Flag=(1)
SET @Var_ReservationDate = @ReservationDate_In
SET @Var_FirstDeptCode = @FirstDeptCode_In

SELECT RTRIM(LBCODE) AS LBCode
      ,RTRIM(LBName) AS LBName
      ,RTRIM(KSAddress) AS Description
  FROM KSCODE (NOLOCK)
 WHERE KSATTRIB IN(1,2,4) AND
       STOPDATE IS NULL AND
       LBCode LIKE '%' + @Var_FirstDeptCode AND
       CODE IN (SELECT KSCODE
                  FROM MZSCHEDULE (NOLOCK)
                 WHERE YSCODE<>'*' AND 
                      (@Var_Flag=0 OR (@Var_Flag=1 AND WEEKDAY=DATEPART(WEEKDAY,@Var_ReservationDate))) AND
                       (STATUS IS NULL OR STATUS = 2)
               )
 GROUP BY RTRIM(LBCODE),RTRIM(LBName)